//
//  ViewController.m
//  ImageSelect
//
//  Created by csq on 15/12/14.
//  Copyright © 2015年 csq. All rights reserved.
//

#import "ViewController.h"

#import "AGImagePickerController.h"
#import "AGIPCToolbarItem.h"


@interface ViewController () <AGImagePickerControllerDelegate> {
    AGImagePickerController *ipc;
}
@property (nonatomic, strong) NSMutableArray *selectedPhotos;
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.selectedPhotos = [NSMutableArray array];
    __block ViewController *blockSelf = self;
    ipc = [AGImagePickerController sharedInstance:self];
    ipc.didFailBlock = ^(NSError *error) {
        NSLog(@"Fail. Error: %@", error);
        if (error == nil) {
            [blockSelf.selectedPhotos removeAllObjects];
            NSLog(@"User has cancelled.");
            [blockSelf dismissViewControllerAnimated:YES completion:nil];
        } else {
            // We need to wait for the view controller to appear first.
            double delayInSeconds = 0.5;
            dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
            dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
                [blockSelf dismissViewControllerAnimated:YES completion:nil];
            });
        }
//[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
        
    };
    ipc.didFinishBlock = ^(NSArray *info) {
        [blockSelf.selectedPhotos setArray:info];
        NSLog(@"Info: %@", info);
        [blockSelf dismissViewControllerAnimated:YES completion:nil];
//[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:YES];
    };
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

- (IBAction)openAction:(id)sender {
    // Show saved photos on top
    ipc.shouldShowSavedPhotosOnTop = NO;
    ipc.selection = self.selectedPhotos; //选择的图片
    ipc.maximumNumberOfPhotosToBeSelected = 9; //最多选择几张图片
    ipc.toolbarItemsForManagingTheSelection = nil;
    
//    // Custom toolbar items
//    AGIPCToolbarItem *selectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"+ Select All" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
//        return YES;
//    }];
//    AGIPCToolbarItem *flexible = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] andSelectionBlock:nil];
//    AGIPCToolbarItem *selectOdd = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"+ Select Odd" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
//        return !(index % 2);
//    }];
//    AGIPCToolbarItem *deselectAll = [[AGIPCToolbarItem alloc] initWithBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"- Deselect All" style:UIBarButtonItemStyleBordered target:nil action:nil] andSelectionBlock:^BOOL(NSUInteger index, ALAsset *asset) {
//        return NO;
//    }];
//    ipc.toolbarItemsForManagingTheSelection = @[selectAll, flexible, selectOdd, flexible, deselectAll];
    
    [self presentViewController:ipc animated:YES completion:nil];
    
    // Show first assets list, modified by springox(20140503)
    [ipc showFirstAssetsController];
    
    //// Show assets list with name, added by springox(20150719)
//    [ipc showAssetsControllerWithName:@"Camera Roll"];
}


//旋转
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAll;
}

#pragma mark - AGImagePickerControllerDelegate
- (NSUInteger)agImagePickerController:(AGImagePickerController *)picker
         numberOfItemsPerRowForDevice:(AGDeviceType)deviceType
              andInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (deviceType == AGDeviceTypeiPad)
    {
        if (UIInterfaceOrientationIsLandscape(interfaceOrientation))
            return 11;
        else
            return 8;
    } else {
        if (UIInterfaceOrientationIsLandscape(interfaceOrientation)) {
            if (480 == self.view.bounds.size.width) {
                return 6;
            }
            return 7;
        } else
            return 4;
    }
}

- (BOOL)agImagePickerController:(AGImagePickerController *)picker shouldDisplaySelectionInformationInSelectionMode:(AGImagePickerControllerSelectionMode)selectionMode
{
    return (selectionMode == AGImagePickerControllerSelectionModeSingle ? NO : YES);
}

- (BOOL)agImagePickerController:(AGImagePickerController *)picker shouldShowToolbarForManagingTheSelectionInSelectionMode:(AGImagePickerControllerSelectionMode)selectionMode
{
    return (selectionMode == AGImagePickerControllerSelectionModeSingle ? NO : YES);
}

- (AGImagePickerControllerSelectionBehaviorType)selectionBehaviorInSingleSelectionModeForAGImagePickerController:(AGImagePickerController *)picker {
    return AGImagePickerControllerSelectionBehaviorTypeRadio;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
